#!/bin/bash -x
echo "beginning wrf sandbox run at `date`"

if [ $# -lt 3 ]
then
    echo "Usage: wrf.sandbox.sh session_name gfs_dir namelist_path"
    exit 1
fi

start_date=$1
gfs_dir=$2
namelist_path=$3

echo "Model simulation time: $start_date"

echo "System time: `date`"
WRF_dir="/public/users/wrf"
HOME_DIR="/public/users/wrf"
WPS_ROOT=${WRF_dir}/WPS
WRF_ROOT=${WRF_dir}/WRFV3
WRFOUT_ROOT=${HOME_DIR}/wrfout
WRFLOG_ROOT=${HOME_DIR}/wrflog
WRFRUN_ROOT=${HOME_DIR}/wrfrun

WRFRUN_dir=${WRFRUN_ROOT}/$start_date
WRFOUT_dir=$WRFOUT_ROOT/$start_date
WRFLOG_dir=$WRFLOG_ROOT/$start_date

if [ -n "$1" ]
then
    if [ -d "$WRFRUN_dir" ]
    then
        echo "WRFRUN Directory already exist, Rewriting!"
#        exit 2
    else
        echo "mkdir $WRFRUN_dir"
        mkdir -p "$WRFRUN_dir"
    fi
    echo "mkdir $WRFOUT_dir"
    mkdir -p "$WRFOUT_dir"
    echo "mkdir $WRFLOG_dir"
    mkdir -p "$WRFLOG_dir"
else
    echo "Usage: $0 YYYYMMDDHH"
    exit 1
fi

echo "============= Beginning WPS Process ============="

cd ${WRFRUN_dir}
pwd

#
# 1. running geogrid.exe to generate geo_em.d0?.nc files
#
echo "begin geogrid.exe at `date`"

rm -f geogrid.log*
rm -f ungrib.log*
rm -f metgrid.log*
rm -f mygeogrid.o*
rm -f mymetgrid.o*
rm -f myungrib.o*
# rm -f geo_em.d*.nc
# rm -f met_em.d*.nc
# rm -f GRIBFILE.*
rm -f PFILE*
# rm -f FILE*

echo "Copy namelists from $namelist_path"
cp -f $namelist_path/namelist.wps ${WRFRUN_dir}/
cp -f $namelist_path/namelist.input ${WRFRUN_dir}/

# backup namelists
cp -f ${WRF_ROOT}/test/em_real/namelist.wps ${WRFLOG_dir}/
cp -f ${WRF_ROOT}/test/em_real/namelist.input ${WRFLOG_dir}/

# copy WPS files to WRFRUN dir
mkdir -p geogrid metgrid
cp -f ${WPS_ROOT}/geogrid.exe .
cp -f ${WPS_ROOT}/link_grib.csh .
cp -f ${WPS_ROOT}/ungrib.exe .
cp -f ${WPS_ROOT}/metgrid.exe .
cp -f ${WPS_ROOT}/Vtable .

# copy WPS TBLs
cp -f ${WPS_ROOT}/geogrid/GEOGRID.TBL geogrid/
cp -f ${WPS_ROOT}/metgrid/METGRID.TBL metgrid/

# copy WRF files to WRFRUN dir
ln -s ${WRF_ROOT}/test/em_real/real.exe .
ln -s ${WRF_ROOT}/test/em_real/wrf.exe .

# copy WRF TBLs
cp -f ${WRF_ROOT}/test/em_real/*.TBL .

# copy torque files
cp -f ${namelist_path}/*.torque .


#
# 1. running geogrid.exe to generate domain files
#
GEOG_JOB_NAME=`qsub geogrid.torque`
while qstat | grep ${GEOG_JOB_NAME}
do
    echo "wait for geogrid process $GEOG_JOB_NAME"
    sleep 10s
done

#
# 2. running ungrib.exe to generate FILE:200?-* files
#
echo "begin ungrib.exe at `date`"

# link GRIBFILEs
./link_grib.csh ${gfs_dir}/*

UNGRIB_JOB_NAME=`qsub ungrib.torque`
while qstat | grep ${UNGRIB_JOB_NAME}
do 
    echo "wait for ungrib process $UNGRIB_JOB_NAME"
    sleep 10s
done

#
# 3. running metgrid.exe to generate met_em.d0?.* files
#
echo "begin metgrid.exe at `date`"
MET_JOB_NAME=`qsub metgrid.torque`
while qstat | grep ${MET_JOB_NAME}
do 
    echo "wait for metgrid process $METGRID_JOB_NAME"
    sleep 10s
done

#
# 4. save all log files to wrflog
#
mv metgrid.log* ungrib.log* geogrid.log* $WRFLOG_dir
rm -f mygeogrid.o* myungrib.o* mymetgrid.o* 
echo "System time: `date`"
echo "============= WPS Process Finished ============="

########### WPS Running finished #################################

########################## run wrf ###############################
echo "============= Beginning WRF Process ============="

#cd ${WRF_ROOT}/test/em_real

rm -f rsl.*
rm -f myreal.o*
rm -f mywrf.o*

#ln -s ${WPS_ROOT}/met_em.d0*.nc .

#
# running real.exe
#
echo "begin real.exe at `date`"
REAL_JOB_NAME=`qsub real.torque`
while qstat | grep ${REAL_JOB_NAME}
do 
    echo "wait for real.exe process $REAL_JOB_NAME"
    sleep 10s
done

mkdir $WRFLOG_dir/real_log
mv rsl.* $WRFLOG_dir/real_log

#
# running wrf.exe
#
echo "begin wrf.exe at `date`"
WRF_JOB_NAME=`qsub wrf.torque`
while qstat | grep ${WRF_JOB_NAME}
do
    echo "wait for wrf.exe process $WRF_JOB_NAME"
    sleep 20s
done

mkdir $WRFLOG_dir/wrf_log
mv rsl.* $WRFLOG_dir/wrf_log

#
# mv WPS and WRF output to $WRFOUT_dir
#
echo "Copying model results to $WRFOUT_dir"
#mkdir $WRFOUT_dir/metgrid
#mv ${WPS_ROOT}/met_em.d*.nc $WRFOUT_dir/metgrid/

#mkdir $WRFOUT_dir/geogrid
#mv ${WPS_ROOT}/geo_em.d*.nc $WRFOUT_dir/geogrid/

#mkdir $WRFOUT_dir/ungrib
#mv ${WPS_ROOT}/FILE\:* $WRFOUT_dir/ungrib/

# make hard links to wrfout files
mv wrfout_d0* $WRFOUT_dir

# delete intermediate files
# rm -f geo_em.d*.nc
# rm -f met_em.d*.nc
# rm -f GRIBFILE.*
rm -f PFILE*
# rm -f FILE*

echo "Model end at System time: `date`"
echo "============= WRF Process Finished ============="


exit 0
